﻿using System;

public class ReSingletonBase
{
    public ReSingletonBase()
    {
    }
}

public class SingletonBase<T> : ReSingletonBase where T : class
{
    private static T _instance;

    public static bool HasInit => _instance != null;

    // private static readonly object syslock = new object();

    public static T GetInstance()
    {
        if (_instance == null)
        {
#if UNITY_EDITOR
            UnityEngine.Profiling.Profiler.BeginSample($"SingletonBase_{typeof(T).Name}");
#endif
            _instance = Activator.CreateInstance<T>();
#if UNITY_EDITOR
            UnityEngine.Profiling.Profiler.EndSample();
#endif
        }
        return _instance;
    }

    public static void Clear() 
    {
        if (_instance != null)
        {
            _instance.GetType().GetMethod("Dispose")?.Invoke(_instance, null);
            _instance = null;
        }
    }


    public static T Instance
    {
        get
        {

            return GetInstance();
        }
    }

}
